package com.fivefu.core.camera.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fivefu.core.camera.entity.DbMonitorWorkorderImage;
import com.fivefu.core.camera.mapper.DbMonitorWorkorderImageMapper;
import com.fivefu.core.camera.services.DbMonitorWorkorderImageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 工单图片 服务实现类
 * </p>
 *
 * @author 
 * @since 2022-09-22 15:43:06
 */
@Service
public class DbMonitorWorkorderImageServiceImpl extends ServiceImpl<DbMonitorWorkorderImageMapper, DbMonitorWorkorderImage> implements DbMonitorWorkorderImageService {

    /**
     * 编辑工单图片信息
     * @param orderId 工单编号
     * @param image 图片信息
     * @param userid 操作用户编号
     */
    public Long editWorkorderImage(Long orderId, DbMonitorWorkorderImage image, String userid){
        LocalDateTime time = LocalDateTime.now();
        image.setOrderId(orderId);
        image.setUpdatedBy(userid);
        image.setUpdatedTime(time);
        if (image.getId() != null){
            baseMapper.updateById(image);
        } else {
            image.setCreatedBy(userid);
            image.setCreatedTime(time);
            image.setIsDelete(0);
            baseMapper.insert(image);
        }
        return image.getId();
    }

    /**
     * 编辑工单图片
     * @param orderId 工单编号
     * @param imageList 图片信息
     * @param userid 操作用户编号
     */
    @Override
    public void editWorkorderImage(Long orderId, List<DbMonitorWorkorderImage> imageList, String userid){
        List<Long> imageIDs = new ArrayList<>();
        if (imageList != null) {
            for (DbMonitorWorkorderImage image : imageList) {
                Long imageId = editWorkorderImage(orderId, image, userid);
                imageIDs.add(imageId);
            }
        }

        //删除多余图片信息
        QueryWrapper<DbMonitorWorkorderImage> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (imageIDs != null && !imageIDs.isEmpty()){
            wrapper.notIn("id", imageIDs);
        }
        wrapper.eq("order_id", orderId);

        DbMonitorWorkorderImage image = new DbMonitorWorkorderImage();
        image.setUpdatedBy(userid);
        image.setIsDelete(1);
        image.setUpdatedTime(LocalDateTime.now());
        baseMapper.update(image, wrapper);
    }


    /**
     *
     * 格式化工单图片信息
     * @param image
     */
    @Override
    public Map<String, Object> formatWorkorderImage(DbMonitorWorkorderImage image) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("fileTime",image.getFileTime());
        map.put("fileUrl",image.getFileUrl());
        map.put("fileType",image.getFileType());
        return map;
    }

    /**
     *
     * 通过工单编号查询 工单图片集合
     * @param orderId 工单编号
     */
    @Override
    public List<DbMonitorWorkorderImage> queryWorkorderImageListByOrderId(Long orderId) {
        QueryWrapper<DbMonitorWorkorderImage> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0).eq("order_id",orderId);
        return baseMapper.selectList(wrapper);
    }
}
